package com.shanghai.shanghaielectric.UI.setting;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.shanghai.shanghaielectric.Constants;
import com.shanghai.shanghaielectric.DB.LitePalHelper;
//import com.shanghai.shanghaielectric.DB.SharepreferenceUtil;
import com.shanghai.shanghaielectric.MyApplication;
import com.shanghai.shanghaielectric.R;
import com.shanghai.shanghaielectric.UI.KeyBoard.MKeyBoard_NumberInput;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import shanghaielectric_lib.Utils.MySystemUtil;
import shanghaielectric_lib.Utils.dwin.Dwin;


/**
 * 设置-系统界面
 */
@SuppressLint("NewApi")
public class SettingSystemFragment extends Fragment {
    final int SHOWBAR = 301;
    final int HIDEBAR = 302;
    @BindView(R.id.setting_oldpsd)
    TextView settingOldpsd;
    @BindView(R.id.setting_newpsd)
    TextView settingNewpsd;
    @BindView(R.id.setting_repeatpsd)
    TextView settingRepeatpsd;
    @BindView(R.id.setting_cancle)
    Button settingCancle;
    @BindView(R.id.setting_change)
    Button settingChange;
    @BindView(R.id.setting_showBar)
    Button settingShowBar;
    private View settingSystemView;
    private Context mContext = null;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        System.out.println("SettingSystem____onAttach");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("SettingSystem____onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        System.out.println("SettingSystem____onCreateView");
        settingSystemView = inflater.inflate(R.layout.setting_system_fragment, container, false);
        mContext = getActivity().getApplicationContext();
//        //隐藏软键盘
//        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
//        if (imm.isActive()){
//            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
//        }

        ButterKnife.bind(this, settingSystemView);
        initView();
        initData();
        initListener();
        //点击其他部位隐藏软键盘
        settingSystemView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {


                InputMethodManager manager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    if(getActivity().getCurrentFocus()!=null && getActivity().getCurrentFocus().getWindowToken()!=null){
                        manager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
                return false;
            }
        });

        return settingSystemView;
    }
    public void initView() {
        if (MySystemUtil.getNavigationState()) {
            settingShowBar.setText("隐藏导航栏");
            settingShowBar.setBackgroundResource(R.drawable.shape_bg_black);
        } else {
            settingShowBar.setText("显示导航栏");
            settingShowBar.setBackgroundResource(R.drawable.shape_bg_gray);
        }
    }

    public void initData() {
    }
    public void initListener() {
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        System.out.println("SettingSystem____onActivityCreated");
    }

    @Override
    public void onStart() {
        super.onStart();
        System.out.println("SettingSystem____onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
//        //隐藏软键盘
//        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
//        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        System.out.println("SettingSystem____onResume");
    }

    @Override
    public void onPause() {
        super.onPause();

        System.out.println("SettingSystem____onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        System.out.println("SettingSystem____onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        System.out.println("SettingSystem____onDestroyView");

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("SettingSystem____onDestroy");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        System.out.println("SettingSystem____onDetach");
    }

    @OnClick({R.id.setting_cancle, R.id.setting_change, R.id.setting_showBar,R.id.setting_oldpsd,R.id.setting_newpsd,R.id.setting_repeatpsd})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.setting_newpsd:
                //输入密码
                MKeyBoard_NumberInput mKeyBoardInputValue = new MKeyBoard_NumberInput();
                mKeyBoardInputValue.setKeyBoardType(MKeyBoard_NumberInput.NUM);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_NumberInput.MkeyBoardListener_numberKeyBoard() {
                    @Override
                    public void onClink(boolean isCorrect, String value) {
                        settingNewpsd.setText(value);
                    }
                });
                break;
            case R.id.setting_oldpsd:
            //输入密码
            MKeyBoard_NumberInput mKeyBoardInputValue_setting_oldpsd = new MKeyBoard_NumberInput();
                mKeyBoardInputValue_setting_oldpsd.setKeyBoardType(MKeyBoard_NumberInput.NUM);
                mKeyBoardInputValue_setting_oldpsd.show(getFragmentManager(), "");
                mKeyBoardInputValue_setting_oldpsd.setMkeyBoardListener(new MKeyBoard_NumberInput.MkeyBoardListener_numberKeyBoard() {
                @Override
                public void onClink(boolean isCorrect, String value) {
                    settingOldpsd.setText(value);
                }
            });
            break;
            case R.id.setting_repeatpsd:
                //输入密码
                MKeyBoard_NumberInput mKeyBoardInputValue_setting_repeatpsd = new MKeyBoard_NumberInput();
                mKeyBoardInputValue_setting_repeatpsd.setKeyBoardType(MKeyBoard_NumberInput.NUM);
                mKeyBoardInputValue_setting_repeatpsd.show(getFragmentManager(), "");
                mKeyBoardInputValue_setting_repeatpsd.setMkeyBoardListener(new MKeyBoard_NumberInput.MkeyBoardListener_numberKeyBoard() {
                    @Override
                    public void onClink(boolean isCorrect, String value) {
                        settingRepeatpsd.setText(value);
                    }
                });
                break;


            case R.id.setting_cancle:
                settingOldpsd.setText("");
                settingNewpsd.setText("");
                settingRepeatpsd.setText("");
                break;
            case R.id.setting_change:
                if (TextUtils.isEmpty(settingOldpsd.getText())) {
                    Toast.makeText(mContext, "请输入旧密码", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(settingNewpsd.getText())) {
                    Toast.makeText(mContext, "请输入新密码", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(settingOldpsd.getText())) {
                    Toast.makeText(mContext, "请输入重复密码", Toast.LENGTH_SHORT).show();
                } else if (Constants.ADMINI_PASSWORDS.equals(settingOldpsd.getText().toString()) |MySystemUtil.checkPassWords(settingOldpsd.getText().toString())) {
                    //旧密码输入正确，判断新密码输入是否相同
                    if (settingNewpsd.getText().toString().equals(settingRepeatpsd.getText().toString())) {
                        Toast.makeText(mContext, "密码修改成功！", Toast.LENGTH_SHORT).show();
                        //新密码保存到配置文件
//                        SharepreferenceUtil.putAndApply(mContext, "user_psd", settingNewpsd.getText().toString());
//                        Constants.USER_PASSWORDS = settingNewpsd.getText().toString();
                        MySystemUtil.setPassWords(settingNewpsd.getText().toString());
                        settingOldpsd.setText("");
                        settingRepeatpsd.setText("");
                        settingNewpsd.setText("");
                        LitePalHelper.insertOperateInfo("“修改用户密码”");
                    }
                } else {

                    Toast.makeText(mContext, "旧密码输入错误，请重新输入！", Toast.LENGTH_SHORT).show();
                }


                break;
            case R.id.setting_showBar:
                if (MySystemUtil.getNavigationState()) {
                    MySystemUtil.setNavigationState(false);
                    Dwin.getInstance().closeBar();
                    Message message = new Message();
                    message.what = SHOWBAR;
                    handler.sendMessage(message);

                } else {
                    MySystemUtil.setNavigationState(true);
                    Dwin.getInstance().showBar();
                    Message message = new Message();

                    message.what = HIDEBAR;
                    handler.sendMessage(message);
                }

                break;
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == SHOWBAR) {
                settingShowBar.setText("显示导航栏");
                settingShowBar.setBackgroundResource(R.drawable.shape_bg_gray);
            } else {
                settingShowBar.setText("隐藏导航栏");

                settingShowBar.setBackgroundResource(R.drawable.shape_bg_black);
            }

        }
    };

}
